//
//  ZBBLocationViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "ZBBLocationViewController.h"
#import "FitHeader.h"
#import "ZBBLocationViewControllViewCell.h"
#import "TwoToOne.h"
#import "ZBBLocationModel.h"
#import "TabBarController.h"
#import "ZBBPlayHistory.h"
#import "CoreDataManager.h"
#import "ZBBRadioViewController.h"
@interface ZBBLocationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)NSString *urlString;
@property(nonatomic, strong)CoreDataManager *manager;
@property(nonatomic, strong)NSMutableArray *array;
@end

@implementation ZBBLocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.view.backgroundColor = [UIColor whiteColor];
   }
-(void)loadView
{
    [super loadView];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collectionView  = [[ UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[ZBBLocationViewControllViewCell class] forCellWithReuseIdentifier:@"locationCell"];
    
}

-(void)getData;
{
    _array = [NSMutableArray array];
    _dataSourceArray = [NSMutableArray array];
    switch (_index) {
        case 0:
            _urlString = @"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&radioType=2&device=android&provinceCode=220000&pageSize=15";
            break;
        case 1:
            _urlString = @"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&radioType=1&device=android&pageSize=15";
            break;
        case 3:
            _urlString = @"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&radioType=3&device=android&pageSize=15";
            break;
        case 1000:
            _urlString = @"http://live.ximalaya.com/live-web/v1/getTopRadiosList?device=android&radioNum=20";
            break;
        default:
            break;
         }
    [TwoToOne getDataByString:_urlString BodyString:nil WithDataBlock:^(id data) {
        NSArray *array = [data objectForKey:@"result"];
        for (NSDictionary *dic in array) {
            [_array addObject:dic];
            ZBBLocationModel *model = [[ZBBLocationModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataSourceArray addObject:model];
            
        }
        [_collectionView reloadData];
        
        
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZBBLocationViewControllViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"locationCell" forIndexPath:indexPath];
    
    cell.zmodel = _dataSourceArray[indexPath.row];
    
    
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.frame.size.width, 90 * FITHeight);
}
-(void)viewWillAppear:(BOOL)animated
{
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
}
-(void)viewDidDisappear:(BOOL)animated

{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
   [ZBBRadioViewController shareInstance].dic = _array[indexPath.row];
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"cell0"];
    [self.navigationController pushViewController:[ZBBRadioViewController shareInstance]animated:YES];
    [[ZBBPlayHistory shareInstance] takeHistoryArrayFromFile:@"ppppp"];
    [[ZBBPlayHistory shareInstance] addDictionary:_array[indexPath.row]];
    [[ZBBPlayHistory shareInstance] WriteToFile:@"ppppp"];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
